package com.javalong.mir.dwz.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.javalong.mir.common.util.QiNiuUtils;
import com.javalong.mir.constant.Constant;
import com.javalong.mir.constant.StatusCode;
import com.javalong.mir.dwz.vo.DwzResponse;
import com.javalong.mir.pojo.Banner;
import com.javalong.mir.service.BannerService;

@Controller
@RequestMapping("dwz/banner")
public class DwzBannerController {
	
	@Autowired
	private BannerService bannerService;
	
	@RequestMapping("list")
	public String list(Model model) {
		List<Banner> bannerList = bannerService.getList(0, Constant.PAGE_SIZE);
		model.addAttribute(bannerList);
		model.addAttribute("totalCount", bannerService.getCount());
		model.addAttribute("currentPage", 1);
		model.addAttribute("numPerPage", Constant.PAGE_SIZE);
		return "banner/list";
	}

	@RequestMapping("listByPage")
	public String listByPage(@RequestParam("pageNum") int pageNum, @RequestParam("numPerPage") int numPerPage,
			Model model) {
		List<Banner> bannerList = bannerService.getBannerList(pageNum - 1, numPerPage);
		model.addAttribute(bannerList);
		model.addAttribute("totalCount", bannerService.getCount());
		model.addAttribute("currentPage", pageNum);
		model.addAttribute("numPerPage", numPerPage);
		return "banner/list";
	}

	@RequestMapping("delete")
	@ResponseBody
	public DwzResponse delete(@RequestParam("id") int id) {
		bannerService.delete(id);
		DwzResponse response = new DwzResponse();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("删除成功!");
		response.setNavTabId("banner_list");
		return response;
	}

	@RequestMapping("addjsp")
	public String addjsp() {
		return "banner/add";
	}

	@RequestMapping("add")
	@ResponseBody
	public DwzResponse add(Banner banner) {
 		bannerService.save(banner);
		DwzResponse response = new DwzResponse();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("添加成功!");
		response.setNavTabId("banner_list");
		response.setCallbackType("closeCurrent");
		return response;
	}

	@RequestMapping("updatejsp")
	public String updatejsp(@RequestParam("id")int id,Model model) {
		Banner banner = bannerService.findBannerById(id);
		if(banner!=null){
			model.addAttribute(banner);
		}
		return "banner/update";
	}

	@RequestMapping("update")
	@ResponseBody
	public DwzResponse update(Banner banner) {
		bannerService.updateBanner(banner);
		DwzResponse response = new DwzResponse();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("更新成功!");
		response.setNavTabId("banner_list");
		response.setCallbackType("closeCurrent");
		return response;
	}


}









